#标准库，用于创建图形用户界面（GUI），支持窗口、按钮、文本框等控件的创建和管理
import tkinter as tk
from tkinter import PhotoImage
#第三方库，用于图像处理，支持图像打开、编辑、保存、格式转换等操作。Pillow 是 PIL 的现代分支，功能更强大且兼容性更好
from PIL import Image, ImageTk, ImageDraw
#导入全局变量
from .Shared import *

class PageZero(tk.Frame):
    def __init__(self, parent):
        super().__init__(parent)
        #界面属性初始化
        self.configure(bg = "black", width = 1200, height = 720, highlightthickness = 0)
        self.pack_propagate(False) 
        
        #加载图像资源
        self.picture = {
            "PageZero": shareddata.get_picture("PageZero")
        }
        #初始化缓冲画布
        self.buffer_image = Image.new("RGBA", (1200, 720), (0, 0, 0, 1)) 
        self.canvas = tk.Canvas(self, width = 1200, height = 720, highlightthickness = 0, bd = 0)
        self.canvas.place(x = 0, y = 0)

        #设置按钮属性                  文本          字体样式 字体     大小  斜体      按钮背景       字体背景      无边框   点击按钮背景仍然为黑色     点击事件跳转到Start洁敏
        self.button = tk.Button(self, text = "START", font = ("Algerian", 50, "italic"), bg = "black", fg = "white", bd = 0, activebackground = "black", command = lambda: parent.show_frame("PageStart"))
        self.button.bind("<Enter>", self.mouse_in)
        self.button.bind("<Leave>", self.mouse_out)
        self.button.place(x = 450, y = 310, width = 300, height = 100)
        
        #动画缓冲
        self.update_buffer(parent)  

    def mouse_in(self, event):
        # 鼠标悬停时改变字体颜色为道奇蓝，beautiful
        self.button.config(fg = "#1E90FF")

    def mouse_out(self, event):
        self.button.config(fg = "#FFFFFF")
    
    def update_buffer(self, parent):
        self.buffer_image.paste((255, 255, 255, 0), [0, 0, 1200, 720])
        self.draw_to_buffer(self.buffer_image, parent)
        self.photo_buffer = ImageTk.PhotoImage(self.buffer_image)
        self.canvas.create_image(0, 0, image = self.photo_buffer, anchor = tk.NW)

    def draw_to_buffer(self, buffer_image, parent):
        draw = ImageDraw.Draw(buffer_image)
        draw.rectangle([0, 0, 1200, 720], fill = (0, 0, 0, 255)) 
        buffer_image.paste(self.picture["PageZero"]["Logo"][0], (300, 60))    
